package cn.pomelo.secondskill.controller;

import cn.pomelo.secondskill.common.AjaxResult;
import cn.pomelo.secondskill.exception.SecondsKillException;
import cn.pomelo.secondskill.model.Orders;
import cn.pomelo.secondskill.service.OrdersService;
import cn.pomelo.secondskill.service.RedisService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Resource
    private OrdersService ordersService;

    @Resource
    private RedisService redisService;

    @GetMapping("/all")
    public AjaxResult all() {
        List<Orders> list = ordersService.findAll();
        return list == null ? AjaxResult.failure("查询失败") : AjaxResult.success(list);
    }

    /**
     * 加入购物车
     *
     * @param userid  用户id
     * @param goodsId 商品id
     * @return 加入是否成功信息
     */
    @RequestMapping("/addCar")
    public AjaxResult sec(Integer userid, Integer goodsId) {
        try {
            Integer addCar = ordersService.addCar(userid, goodsId);
            return addCar > 0 ? AjaxResult.success() : AjaxResult.failure("添加失败");
        } catch (SecondsKillException e) {
            return AjaxResult.failure(e.getMessage());
        }
    }

    /**
     * 通过用户id 查询订单
     *
     * @param userid 用户id
     * @return 该用户所有订单
     */
    @GetMapping("/getByUserid")
    public AjaxResult getOrdersByUserid(Integer userid) {
        List<Orders> ordersList = redisService.getOrdersByUserid(userid);
        return ordersList == null ? AjaxResult.failure("查询失败") : AjaxResult.success(ordersList);
    }

    /**
     * 支付
     *
     * @param record 支付的订单详情
     * @return 支付是否成功
     */
    @PostMapping("/pay")
    public AjaxResult payOrder(@RequestBody Orders record) {
        int update = ordersService.updateByPrimaryKeySelective(record);
        return update > 0 ? AjaxResult.success() : AjaxResult.failure("支付失败");
    }

    /**
     * 修改订单信息
     *
     * @param record 要修改的订单详情
     * @return 修改是否成功
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Orders record) {
        int i = ordersService.updateByPrimaryKeySelective(record);
        return i > 0 ? AjaxResult.success() : AjaxResult.failure("修改失败");
    }

    /**
     * 通过订单编号获取订单详情
     *
     * @param id 订单id
     * @return 订单详情
     */
    @GetMapping("/getOne")
    public AjaxResult getOne(String id) {
        Orders orders = ordersService.selectByPrimaryKey(id);
        return orders == null ? AjaxResult.failure("查询失败") : AjaxResult.success(orders);
    }

    /**
     * 按照条件查询订单
     *
     * @param userid 用户id
     * @param state  订单状态码 0 未支付 1 已支付 2 订单失效 3 仅加入购物车
     * @return 所有符合条件的订单
     */
    @RequestMapping("/condition")
    public AjaxResult getByState(Integer userid, Integer state) {
        Orders record = new Orders();
        record.setState(state);
        record.setUserId(userid);
        List<Orders> list = ordersService.findByCondition(record);
        return list != null ? AjaxResult.success(list) : AjaxResult.failure("查询失败");
    }

    /**
     * 根据订单编号删除订单
     *
     * @param id 订单编号
     * @return 是否删除成功
     */
    @RequestMapping("/remove")
    public AjaxResult remove(String id) {
        int i = ordersService.deleteByPrimaryKey(id);
        return i > 0 ? AjaxResult.success() : AjaxResult.failure("删除失败");
    }

}
